<template>
  <view class="comment-item">
    <view class="itemHead">
      <view class="left">
        <image :src="commentDetail.author.avatar" class="avatar"></image>
        <view class="info">
          <view class="authorName">{{ commentDetail.author.nickName }}</view>
          <view class="time">{{
            $formatTime.formatTimeToHHMM(commentDetail.time)
          }}</view>
        </view>
      </view>
      <view class="right" @click="likeComment(commentDetail.id)">
        <image
          :src="isLikeComment ? likedIcon.active : likedIcon.icon"
          class="icon"
        ></image>
        <view>{{ commentDetail.liked }}</view>
      </view>
    </view>
    <view class="comment">{{ commentDetail.content }}</view>
  </view>
</template>

<script>
export default {
  name: "CommentItem",
  props: {
    commentDetail: {
      type: Object,
      required: true,
    },
  },

  data() {
    return {
      isLikeComment: false,
      likedIcon: {
        icon: "../../static/icons/like.png",
        active: "../../static/icons/likeActive.png",
      },
    };
  },
  methods: {
    likeComment(id) {
      if (!this.isLikeComment) {
        try {
          this.$store.dispatch("m_discussion/likeDiscussionComment", id);
          uni.showToast({
            icon: "success",
            title: "点赞成功",
            duration: 1000,
          });
          this.isLikeComment = true;
        } catch (err) {
          console.log("点赞失败：", err);
        }
      } else {
        uni.showToast({
          icon: "error",
          title: "已点过赞",
          duration: 1000,
        });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.comment-item {
  display: flex;
  flex-direction: column;
  margin-top: 20rpx;
  margin-bottom: 30rpx;
  .itemHead {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      display: flex;
      .avatar {
        width: 80rpx;
        height: 80rpx;
        border-radius: 50%;
        margin-right: 10rpx;
      }
      .info {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .authorName {
          font-weight: 500;
        }
        .time {
          font-size: 24rpx;
          color: #aaa;
        }
      }
    }
    .right {
      display: flex;
      align-items: center;
      font-size: 26rpx;
      color: #aaa;

      .icon {
        width: 40rpx;
        height: 40rpx;
        margin-right: 5rpx;
      }
    }
  }
  .comment {
    margin-top: 15rpx;
    font-size: 28rpx;
  }
}
</style>
